/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ final class ItemPreloadingGrouper
/*     */   extends BufferingResultsGrouper
/*     */ {
/*     */   private final int chunkSize;
/*     */   private final boolean inDumpster;
/*     */   private final OperationContext opContext;
/*     */   
/*     */   ItemPreloadingGrouper(ZimbraQueryResults results, int chunkSize, Mailbox mbox, boolean inDumpster)
/*     */   {
/*  41 */     super(results);
/*  42 */     assert (chunkSize > 0);
/*  43 */     this.chunkSize = chunkSize;
/*  44 */     this.opContext = mbox.getOperationContext();
/*  45 */     this.inDumpster = inDumpster;
/*     */   }
/*     */   
/*     */   protected boolean bufferHits() throws ServiceException
/*     */   {
/*  50 */     if (this.bufferedHit.size() > 0) {
/*  51 */       return true;
/*     */     }
/*     */     
/*  54 */     if (!this.hits.hasNext()) {
/*  55 */       return false;
/*     */     }
/*     */     
/*  58 */     ArrayList<ZimbraHit> toLoad = new ArrayList();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  64 */     ZimbraHit firstHit = this.hits.peekNext();
/*  65 */     Mailbox mbx = firstHit.getMailbox();
/*     */     
/*  67 */     int numLoaded = 0;
/*     */     do {
/*  69 */       ZimbraHit nextHit = this.hits.getNext();
/*  70 */       this.bufferedHit.add(nextHit);
/*     */       
/*  72 */       if ((nextHit.getMailbox() == mbx) && (mbx != null)) {
/*  73 */         toLoad.add(nextHit);
/*     */       }
/*     */       
/*  76 */       numLoaded++;
/*  77 */     } while ((numLoaded < this.chunkSize) && (this.hits.hasNext()));
/*     */     
/*  79 */     preload(mbx, toLoad);
/*     */     
/*  81 */     return true;
/*     */   }
/*     */   
/*     */   private void preload(Mailbox mbox, List<ZimbraHit> hits) throws ServiceException {
/*  85 */     int[] unloadedIds = new int[hits.size()];
/*  86 */     int numToLoad = 0;
/*  87 */     for (int i = 0; i < hits.size(); i++) {
/*  88 */       ZimbraHit cur = (ZimbraHit)hits.get(i);
/*  89 */       if (!cur.itemIsLoaded()) {
/*  90 */         numToLoad++;
/*  91 */         unloadedIds[i] = cur.getItemId();
/*     */       } else {
/*  93 */         unloadedIds[i] = -1;
/*     */       }
/*     */     }
/*     */     
/*  97 */     if (numToLoad > 0) {
/*  98 */       MailItem[] items = mbox.getItemById(this.opContext, unloadedIds, MailItem.Type.UNKNOWN, this.inDumpster);
/*  99 */       for (int i = 0; i < hits.size(); i++) {
/* 100 */         if (items[i] != null) {
/* 101 */           ((ZimbraHit)hits.get(i)).setItem(items[i]);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/ItemPreloadingGrouper.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */